/*
 * ServerIntermediate.java
 *
 * Created on January 29, 2007, 9:55 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.server;

import net.sf.iSnake.core.postmodeISCPpacketForSend;

/**
 *
 * @author Suraj
 */
public class ServerIntermediate extends Thread{
    private ServerCorePre scp;
    private int totalClientsCounts;
    private int verifiedClientsCounts;
    private boolean handle;
    private Object obj;
    private int client;
    
    /** Creates a new instance of ServerIntermediate */
    public ServerIntermediate(ServerCorePre scp,int no) {
        this.scp=scp;
        this.totalClientsCounts=no;
        this.verifiedClientsCounts=0;
        this.handle=false;
    }
    
    public synchronized void run(){
        postmodeISCPpacketForSend packet;
        scp.status(ServerStatus.DEBUG_MESSAGE,"Total Clients to be veryfied :"+ totalClientsCounts);
        while(!scp.isPreMode() && !scp.isPostMode()){
            if(this.handle){
                //packet=(postmodeISCPpacketForSend) obj;
                verifiedClientsCounts++;
                scp.status(ServerStatus.DEBUG_MESSAGE,"Switched to post Mode Client :"+ client);
                if(verifiedClientsCounts==totalClientsCounts){
                    scp.setPostMode(true);
                    scp.getServerPost().start();
                    //this thread will automatically stop after this
                }
                handle=false;
                notifyAll();
            } else {
                try {
                    wait();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    public synchronized void forwardSignal(int c,Object obj){
        while(true){
            if(!handle){
                this.client=c;
                this.obj=obj;
                this.handle=true;
                break;
            } else{
                try {
                    wait();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
        notifyAll();
    }
}
